package com.dhecp.project.system.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.dhecp.common.core.domain.AjaxResult;
import com.dhecp.common.utils.StringUtils;
import com.dhecp.core.constant.DataFiltering;
import com.dhecp.core.utils.PageUtils;
import com.dhecp.project.system.domain.SysDictionaryForModule;
import com.dhecp.project.system.mapper.SysDictionaryForModuleMapper;
import com.dhecp.project.system.service.SysDictionaryForFieldService;
import com.dhecp.project.system.service.SysDictionaryForModuleService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;


/**
 * 功能数据字典  业务实现层
 * @author DH-Xxj
 * @version 2019年9月24日
 */
@Service
public class SysDictionaryForModuleServiceImpl implements SysDictionaryForModuleService {

	@Autowired
	private SysDictionaryForModuleMapper dictionaryForModuleMapper;

	@Autowired
	private SysDictionaryForFieldService dictionaryForFieldService;

	/**
	 * 通过guid查询单条记录
	 * @param guid
	 * @return 实体 SysDictionaryForModule
	 * @author DH-Xxj
	 */
	@Override
	public SysDictionaryForModule queryDictionaryForModuleByGuid(String dictId){
		return dictionaryForModuleMapper.findDictionaryForModuleByGuid(dictId);
	}

	/**
	 * 按功能唯一标识查询
	 * @param moduleTable
	 * @return 实体 SysDictionaryForModule
	 * @author DH-Xxj
	 */
	@Override
	public SysDictionaryForModule queryDictionaryForModuleByModuleTable(String moduleTable){
		return dictionaryForModuleMapper.findDictionaryForModuleByModuleTable(moduleTable);
	}
	
	/**
	 * 查询所有功能数据字典列表
	 * @param params
	 * @return List-SysDictionaryForModule
	 * @author DH-Xxj
	 * @throws Exception 
	 */
	@Override
	public List<SysDictionaryForModule> queryDictionaryForModule(Map<String, Object> params) throws Exception {		
		params.put(DataFiltering.DATA_WHERE, dictionaryForFieldService.getWhereStr(params));
		if (params.get("field") == null || params.get("field") == ""){
			String orderBy = "menuGuid desc, moduleOrder asc";
			PageUtils.startPageOrder(orderBy);
		}else{
			PageUtils.startPage();	
		}	
		return dictionaryForModuleMapper.findDictionaryForModule(params);
	}
	
	/**
	 * 按查询条件获取功能数据字典列表
	 * @param params
	 * @return List-SysDictionaryForModule
	 * @author DH-Xxj
	 * @throws Exception 
	 */
	@Override
	public List<SysDictionaryForModule> queryDictionaryForModuleList(Map<String, Object> params) throws Exception {
		params.put(DataFiltering.DATA_WHERE, dictionaryForFieldService.getWhereStr(params));		//常规查询条件SQL语句
//		params.put(DataFiltering.DATA_SCODE, dictionaryForFieldService.getDataScode(params));		//数据权限查询条件SQL语句
		return dictionaryForModuleMapper.findDictionaryForModuleList(params);
	}
	
	/**
	 * 需要身份验证的功能
	 * @param params
	 * @return Map (ModuleTable,PasswordAuthentication)
	 * @author DH-Xxj
	 */
	@Override
	public Map<String, String> queryAuthenticationModule(Map<String, Object> params){
		Map<String, String> authenticationModuleMap = new HashMap<String, String>();
		List<SysDictionaryForModule> dictionaryForModuleList = dictionaryForModuleMapper.findAuthenticationModuleList(params);
		
		if (dictionaryForModuleList != null && dictionaryForModuleList.size() != 0){
			for (SysDictionaryForModule dictionaryForModule : dictionaryForModuleList){
				authenticationModuleMap.put(dictionaryForModule.getModuleTable().toUpperCase(), dictionaryForModule.getPasswordAuthentication().toLowerCase());
			}
		}
		return authenticationModuleMap;
	}

	/**
	 * 新增功能数据字典
	 * @param dictionaryForModuleItems
	 * @return MessageModel
	 * @author DH-Xxj
	 */
	@Override
	public AjaxResult insertDictionaryForModule(Map<String, Object> params) {
		if (StringUtils.isEmpty(params)) {
			return AjaxResult.error("数据提交失败，必填参数有误！");
		}
		
		List<SysDictionaryForModule> dictionaryForModuleList = JSON.parseArray(params.get("dictionaryForModuleItems").toString(), SysDictionaryForModule.class);
		
		if (dictionaryForModuleList == null || dictionaryForModuleList.size() == 0) {
			return AjaxResult.error("数据包格式有误，请检查！");
		}
		for (SysDictionaryForModule dictionaryForModule : dictionaryForModuleList) {
			if (dictionaryForModuleMapper.findDictionaryForModuleByModuleTable(dictionaryForModule.getModuleTable()) != null){
	            return AjaxResult.error("功能唯一标识【" + dictionaryForModule.getModuleTable() + "】已经存在，不能重复添加！");
	        }else {
	        	
	        	dictionaryForModuleMapper.insertDictionaryForModule(dictionaryForModule);
			}
		}
		return AjaxResult.success("操作成功");
	}

	/**
	 * 通过guid删除功能数据字典
	 * @param dictionaryForModuleItems
	 * @return MessageModel
	 * @author DH-Xxj
	 */
	@Override
	public AjaxResult deleteDictionaryForModuleByGuid(Map<String, Object> params) {
		List<SysDictionaryForModule> dictionaryForModuleList = JSON.parseArray(params.get("dictionaryForModuleItems").toString(), SysDictionaryForModule.class);
		if (dictionaryForModuleList == null || dictionaryForModuleList.size() == 0) {
			return AjaxResult.error("数据包格式有误，请检查！");
		}
		for (SysDictionaryForModule dictionaryForModule : dictionaryForModuleList) {
			dictionaryForModuleMapper.deleteByGuid(dictionaryForModule);
		}
		return AjaxResult.success("操作成功");
	}

	/**
	 * 通过guid修改功能数据字典
	 * @param dictionaryForModuleItems
	 * @return MessageModel
	 * @author DH-Xxj
	 */
	@Override
	public AjaxResult updateDictionaryForModuleByGuid(Map<String, Object> params) {
		if (StringUtils.isEmpty(params)) {
			return AjaxResult.error("数据提交失败，必填参数有误！");
		}
		List<SysDictionaryForModule> dictionaryForModuleList = JSON.parseArray(params.get("dictionaryForModuleItems").toString(), SysDictionaryForModule.class);
		if (dictionaryForModuleList == null || dictionaryForModuleList.size() == 0) {
			return AjaxResult.error("数据包格式有误，请检查！");
		}
		for (SysDictionaryForModule dictionaryForModule : dictionaryForModuleList) {
			dictionaryForModuleMapper.updateByGuid(dictionaryForModule);
		}
		return AjaxResult.success("操作成功");
	}

}
